﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using System.Windows.Controls.Data;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCB.UI.PopWindows;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls;
using Baosight.Platform.Core.ServiceModel;
using Baosight.Platform.Core.UI.Controls;
using System.Collections.ObjectModel;
using GanttView.Charts.Common.Interface;
using GanttView.Charts.Common.Instance;
using GanttView.Charts.Enum;
using GanttView.Charts.Common.ItemClass;
using GanttView.Charts.Common;
using GanttView.Charts;
using System.Text.RegularExpressions;

namespace Baosight.ZNH.ZMCB.UI
{
    public partial class ZMCBC1 : EFViewCmes
    {
        private static string uniqueIdPath = "fdKeyId", taskNamePath = "fdPlace", startDatePath = "fdBeginDate", endDatePath = "fdEndDate", percentCompletePath = "fdComPercent";
        public ZMCBC1()
        {
            InitializeComponent();
            this.Gantt.Loaded += new RoutedEventHandler(Gantt_Loaded);
            //this.RESULT.LayoutUpdated += new EventHandler(RESULT_LayoutUpdated);
        }

        
        /// <summary>
        /// 初次进入控制按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //private void RESULT_LayoutUpdated(object sender, EventArgs e)
        //{
        //    if (this.status.Text == null)
        //        return;

        //    ControlButton(this.status.Text);
        //}

        /// <summary>
        /// 加载Gantt图的数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Gantt_Loaded(object sender, RoutedEventArgs e)
        {
            this.Gantt.TreeGrid.PreparingCellForEdit += new EventHandler<DataGridPreparingCellForEditEventArgs>(TreeGrid_PreparingCellForEdit);
            this.Gantt.Update(this.Gantt.StartDate);
            UpdateGanttData();
        }


        /// <summary>
        /// 更新Gantt图页面数据
        /// </summary>
        private void UpdateGanttData()
        {
            if (this.EIDataContext.ContainsBlockName("result"))
            {
                this.Gantt.DataSource = GanttView.Charts.StaticTools.GetTasklistByBlock(this.EIDataContext.GetBlock("result"), uniqueIdPath, taskNamePath, startDatePath, endDatePath, percentCompletePath);
                this.Gantt.Update();
            }
        }

        /// <summary>
        /// 在编辑紧前任务前准备block
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TreeGrid_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
        {
            if ("紧前任务".Equals(e.Column.Header))
            {
                this.EIDataContext.GetBlock("cmes.cb.frontPlaces").Rows.RemoveAll();
                ObservableCollection<ITaskDescriptor> tasks = this.Gantt.DataSource as ObservableCollection<ITaskDescriptor>;
                foreach (ITaskDescriptor task in tasks)
                {
                    if (!task.Equals((e.Row.DataContext as TaskEntryLayoutItem).Data))
                    {
                        EIRow row = new EIRow();
                        row["fdPlace"] = task.TaskName;
                        row["fdPlaceId"] = task.ExtendAttributes["fdPlaceId"];
                        this.EIDataContext.GetBlock("cmes.cb.frontPlaces").AddRow(row);
                    }
                }
                EIRow row0 = new EIRow();
                row0["fdPlace"] = "";
                row0["fdPlaceId"] = "";
                this.EIDataContext.GetBlock("cmes.cb.frontPlaces").AddRow(row0);
            }
        }
        

        /// <summary>5
        /// 初始化
        /// </summary>
        /// <param name="initData"></param>
        public override void Initialize(Baosight.Platform.Core.EIInfo initData)
        {
            
            base.Initialize(initData);
            
        }

        

    }
}
